package com.time.company.components.task;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.time.company.R;
import com.time.company.base.BaseRecAdapter;
import com.time.company.base.BaseViewHolder;
import com.time.company.servermodel.task.TaskData;
import com.time.company.utils.ScreenUtils;
import com.time.company.webserver.helper.picasso.PicassoImageLoader;

import java.util.List;

/**
 * 任务 - 志愿活动
 * Created by Bing on 2017/11/9.
 * E-mail is tx16bing@163.com
 */

public class RecentAdapter extends BaseRecAdapter<TaskData.RecentActArray> {

    public RecentAdapter(Context mContext, List<TaskData.RecentActArray> list, int viewTypeCount) {
        super(mContext, list, viewTypeCount);
    }

    @Override
    public BaseViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View convertView = inflater.inflate(R.layout.item_task_recent, parent, false);
        RecentViewHolder recentViewHolder = new RecentViewHolder(convertView);
        convertView.setTag(recentViewHolder);
        return recentViewHolder;
    }

    @Override
    public void onBindViewHolder(BaseViewHolder holder, final int position) {
        final TaskData.RecentActArray recentActArray = getItem(position);
        RecentViewHolder recentHolder = (RecentViewHolder) holder;
        if (!TextUtils.isEmpty(recentActArray.getCoverPicUrl())) {
            int width = ScreenUtils.dip2px(88);
            int height = ScreenUtils.dip2px(68);
            PicassoImageLoader.loadImageViewSize(mContext,
                    recentActArray.getCoverPicUrl(),
                    width,
                    height,
                    R.drawable.iv_default_92_68,
                    R.drawable.iv_default_92_68,
                    toString(), recentHolder.ivPic);
        }
        recentHolder.tvCity.setText(recentActArray.getCity());
        recentHolder.tvTitle.setText(recentActArray.getTitle());
        recentHolder.tvSubTitle.setText(recentActArray.getIntroduction());

        if (position == mList.size() - 1) {
            recentHolder.divider.setVisibility(View.GONE);
        } else {
            recentHolder.divider.setVisibility(View.VISIBLE);
        }

        recentHolder.llParent.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (null != onItemClickListener){
                    if (TextUtils.isEmpty(recentActArray.getId())){
                        return;
                    }else {
                        onItemClickListener.onItemClick(v, position, recentActArray);
                    }
                }
            }
        });

        if (position == getItemCount()-1){
            recentHolder.divider.setVisibility(View.GONE);
        }else {
            recentHolder.divider.setVisibility(View.VISIBLE);
        }

    }

    class RecentViewHolder extends BaseViewHolder {

        public LinearLayout llParent;
        public ImageView ivPic;
        public TextView tvCity;
        public TextView tvTitle;
        public TextView tvSubTitle;
        public View divider;

        public RecentViewHolder(View itemView) {
            super(itemView);
            llParent = getView(R.id.ll_recent_item_parent);
            ivPic = getView(R.id.iv_recent_pic);
            tvCity = getView(R.id.tv_recent_location);
            tvTitle = getView(R.id.tv_recent_title);
            tvSubTitle = getView(R.id.tv_recent_sub_title);
            divider = getView(R.id.v_divider);
        }
    }

}
